var treeObj;
var chartEC;

$(function() {
		tree_init();
});

// tree初始化
var tree_init = function() {
	console.log(new Date);
	// 获取油气井数据
	$.ajax({
		url : '/gas/dynamic/tree/tree',
		dataType : 'json'
	}).done(function(data) {
		if (!data) {
			return;
		}

		// 初始化树
		var settings = {
			treeId : "mytree",
			check : {
				enable : true,
				chkboxType : {
					"Y" : "",
					"N" : "s"
				}
			},
			view : {
				showIcon : false,
				fontCss : {
					font_size : "13px"
				}
			},
			callback : {
				onClick : myOnClick,
				onCheck : myOnCheck
			}
		};

		treeObj = $.fn.zTree.init($('#xzqtree'), settings, data);
		console.log(new Date);

		// 节点点击的处理
		function myOnClick(event, treeId, treeNode) {
//			alert(treeNode.wellcode+"||"+treeNode.text+"||"+treeNode.id);

			if (treeNode.chkDisabled == false) {

				if (treeNode.checked == true) {
					treeNode.checked = false;
				} else {
					treeNode.checked = true;
				}
				treeObj.updateNode(treeNode);
				isChartButtonShow();
			}

		};
		// 节点checked的处理
		function myOnCheck(event, treeId, treeNode) {
//			alert(treeNode.wellcode+"||"+treeNode.text+"||"+treeNode.id);
			isChartButtonShow();
		};

	});
}

var isChartButtonShow = function() {
	var showButton = false;
	var level = $("#chart-select").children('option:selected').val();
	
	if (treeObj != null) {
		if (level != -1) {
			var nodes = treeObj.getCheckedNodes(true);
			// alert("length："+nodes.length);
			for (var i = 0; i < nodes.length; i++) {
				if (nodes[i].level == level) {
					showButton = true;
					break;
				}
			}

			if (showButton) {
				$("#showChartBtn").removeAttr("disabled");// 要变成Enable，JQuery只能这么写
			} else {
				$("#showChartBtn").attr("disabled", "disabled");// 再改成disabled
			}

		} else {
			var nodes = treeObj.getNodes();
			var nodes_array = treeObj.transformToArray(nodes);
			for (var i = 0; i < nodes_array.length; i++) {
				treeObj.checkNode(nodes_array[i], false, true);
				treeObj.setChkDisabled(nodes_array[i], true);
			}
		}
	}
}
